/* function BFS(){
    let queue= [入口坐标]

    while(queue.length){
        let cur = queue.shift()
        //审查 top
        // 站在top 坐标上能看到的所有坐标全部记录下来
        for(top){

        }
    }{

    }
} */
const root = {
    val:'A',
    left:{
        val:'B',
        left:{
            val:'D'
        },
        right:{
            val:'E'
        }
    },
    right:{
        val:'C',
        left:{val:'F'},
        right:{val:'G'}
    }
}
/* function preorder(root){
    if(!root) return;
    console.log(root.val);
    preorder(root.left);
    preorder(root.right);
} */
//输入：root = [3,9,20,null,null,15,7]输出：[[3],[9,20],[15,7]]
    var levelOrder = function(root) {
        if(!root) return root
        const queue = [root]
        const res =[] 
        while(queue.length){
            const level=[];
            const len = queue.length
            for(let i=0;i<len;i++){
                const top =queue.shift()
                level.push(top.val)
               if (top.left){
                queue.push(top.left)
               }
                if(top.right){
                    queue.push(top.right)
                }
            }
            res.push(level)
        }
        return res
    };

/*     var levelOrder = function(root) {
        const res = []
          if (!root) return res
        
          const queue = []
          queue.push(root)
        
          while(queue.length) {
            const level = []
            const len = queue.length
        
            for (let i = 0; i < len; i++) {
              const top = queue.shift()
              level.push(top.val)
              if (top.left) {
                queue.push(top.left)
              }
              if (top.right) {
                queue.push(top.right)
              }
            }
        
            res.push(level)
          }
        
          return res
        }; */
console.log(levelOrder(root));
